package com.fei.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fei.springboot.domain.User;
import com.fei.springboot.service.UserService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;

@Api(tags = "测试接口")
@RestController
@RequestMapping("/user")
public class UserController
{
    
    @Autowired
    private UserService userService;
    
    @GetMapping("/hello")
    public String hello()
    {
        return "hello";
    }
    
    /**
     * 测试插入
     * 
     * @return
     */
    @PostMapping("/add")
    public String add(String id, String userName)
    {
        User u = new User();
        u.setId(id);
        u.setUserName(userName);
        this.userService.insertUser(u);
        return u.getId() + "    " + u.getUserName();
    }
    
    /**
     * 测试读
     * 
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public String findById(@PathVariable("id") String id)
    {
        User u = this.userService.findById(id);
        return u.getId() + "    " + u.getUserName();
    }
    
    /**
     * 测试写然后读
     * 
     * @param id
     * @param userName
     * @return
     */
    @PostMapping("/addAndRead")
    public String addAndRead(String id, String userName)
    {
        User u = new User();
        u.setId(id);
        u.setUserName(userName);
        this.userService.wirteAndRead(u);
        return u.getId() + "    " + u.getUserName();
    }
    
    /**
     * 测试读然后写
     * 
     * @param id
     * @param userName
     * @return
     */
    @PostMapping("/readAndAdd")
    public String readAndWrite(String id, String userName)
    {
        User u = new User();
        u.setId(id);
        u.setUserName(userName);
        this.userService.readAndWirte(u);
        return u.getId() + "    " + u.getUserName();
    }
    
    /**
     * 测试分页插件
     * 
     * @return
     */
    @GetMapping("/queryPage")
    public String queryPage()
    {
        PageInfo<User> page = this.userService.queryPage("tes", 1, 2);
        StringBuilder sb = new StringBuilder();
        sb.append("<br/>总页数=" + page.getPages());
        sb.append("<br/>总记录数=" + page.getTotal());
        for (User u : page.getList())
        {
            sb.append("<br/>" + u.getId() + "      " + u.getUserName());
        }
        System.out.println("分页查询....\n" + sb.toString());
        return sb.toString();
    }
    
}
